package org.ndot.java.text;

import java.text.CollationKey;
import java.text.Collator;
import java.text.RuleBasedCollator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

/**
 * 【小蚂蚁学堂 - J2SE_Study】
 * 
 * 项目名称：J2SE_Study
 * 
 * 文件名：TestCollator.java
 * 
 * 功 能:
 * 
 * 
 * 作 者: SunJincheng
 * 
 * 创建时间: 2010-4-7
 * 
 */
public class TestCollator {

	public static void test1() {
		// Compare two strings in the default locale
		Collator myCollator = Collator.getInstance();
		if (myCollator.compare("abc", "ABC") < 0) {
			System.out.println("abc is less than abc");
		} else {
			System.out.println("abc is greater than or equal to abc");
		}
	}

	@SuppressWarnings("unchecked")
	public static void test2() {
		List list = new ArrayList();

		list.add("北京辦公室");
		list.add("台北辦公室");
		list.add("台南辦公室");
		list.add("上海辦公室");

		Collections.sort(list, new Comparator() {
			public int compare(Object o1, Object o2) {
				String s1 = (String) o1;
				String s2 = (String) o2;
				Collator collator = Collator.getInstance(Locale.CHINESE);
				CollationKey key1 = collator.getCollationKey(s1);
				CollationKey key2 = collator.getCollationKey(s2);
				return key1.compareTo(key2);
			}
		});
		System.out.println(list);

	}

	public static void test3() throws Exception {
		String[] words = new String[] { "g", "a", "w", "y", "b" };
		System.out.print("排序前：");
		for (String tem : words)
			System.out.print(tem);
		System.out.println("\n===========");
		String rule = "< a< b< k< d< e< g< h< i< l< m< n< o< p< r< s< t< u< w< y";
		Arrays.sort(words, new RuleBasedCollator(rule));
		System.out.print("排序后：");
		for (String tem : words)
			System.out.print(tem);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			test1();
			test2();
			test3();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
